/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package wf3.servlet;

import acceso.util.Escritor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import wf3.core.Configuration;
import wf3.kernel.WorkflowFactory;

/**
 *
 * @author romulogalindo
 */
public class ConfiguratorServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=ISO-8859-1");
        PrintWriter out = response.getWriter();
        try {
            int accion = Integer.parseInt(request.getParameter("xact"));
            switch (accion) {
                case 1: {
                    // <editor-fold defaultstate="collapsed" desc="CASE 1">
                    boolean ms_login = Boolean.parseBoolean(request.getParameter("ms_login"));
                    String ms_host = request.getParameter("ms_host");
                    boolean wf_filter = Boolean.parseBoolean(request.getParameter("wf_filter"));
                    String wf_filter_host = request.getParameter("wf_filter_host");
                    String wf_modini = request.getParameter("wf_modini");
                    boolean wf_reply = Boolean.parseBoolean(request.getParameter("wf_reply"));
                    String wf_reply_script = request.getParameter("wf_reply_script");

                    String cookie_name = request.getParameter("cookie_name");
                    String cookie_domain = request.getParameter("cookie_domain");

                    Configuration cfg = (Configuration) getServletContext().getAttribute("CFG");
                    Properties prop = cfg.getProperties();
                    prop.setProperty("workflow.master_login", "" + ms_login);
                    prop.setProperty("workflow.master_host", ms_host);
                    prop.setProperty("workflow.filter", "" + wf_filter);
                    prop.setProperty("workflow.host", wf_filter_host);
                    prop.setProperty("workflow.modini", wf_modini);
                    prop.setProperty("workflow.replicacion", "" + wf_reply);
                    prop.setProperty("workflow.replicacion_script", wf_reply_script);
                    prop.setProperty("workflow.login.cookie.name", cookie_name);
                    prop.setProperty("workflow.master_login_cookie_domain", cookie_domain);

                    FileOutputStream f = new FileOutputStream(getServletContext().getRealPath("/WEB-INF/config/acceso_produccion.properties"));
                    System.out.println("Aqui:>>>" + getServletContext().getRealPath("/WEB-INF/config/acceso_produccion.properties"));
                    prop.store(f, null);
                    f.close();
                    cfg.setProperties(prop);
                    getServletContext().setAttribute("CFG", cfg);
                    WorkflowFactory.updateConfiguration(cfg);
                    acceso.filterX.Pasarela.update_filter(wf_filter, wf_filter_host);
                    acceso.filterX.CookieFilter.update_filter(cookie_name);
                    acceso.filterX.DummieFilter.update_filter(cookie_name, cookie_domain);
                    out.println("{\"result\":\"OK\",\"message\":\"Datos grabados correctamente\"}");
                    // </editor-fold>
                    break;
                }
                case 2: {
                    // <editor-fold defaultstate="collapsed" desc="CASE 2">
                    String mst_no_nombre = request.getParameter("mst_no_nombre");
                    String str_mst_ls_items = request.getParameter("str_mst_ls_items");
                    String co_system = request.getParameter("co_system");
                    String no_system = request.getParameter("no_system");

                    String cnt_titulo = request.getParameter("cnt_titulo");
                    String cnt_pag_width = request.getParameter("cnt_pag_width");
                    String cnt_pag_t1_width = request.getParameter("cnt_pag_t1_width");
                    String cnt_pag_t2_width = request.getParameter("cnt_pag_t2_width");

                    Configuration cfg = (Configuration) getServletContext().getAttribute("CFG");
                    Properties prop = cfg.getProperties();
                    prop.setProperty("app.master.no_nombre", "" + mst_no_nombre);
                    prop.setProperty("app.master.ls_items", str_mst_ls_items);
                    prop.setProperty("app.co_system", co_system);
                    prop.setProperty("app.no_system", no_system);
                    prop.setProperty("contenedor.titulo", cnt_titulo);
                    prop.setProperty("contenedor.pagina.width", "" + cnt_pag_width);
                    prop.setProperty("contenedor.pagina.formulario.width_left", cnt_pag_t1_width);
                    prop.setProperty("contenedor.pagina.formulario.width_right", cnt_pag_t2_width);

                    FileOutputStream f = new FileOutputStream(getServletContext().getRealPath("/WEB-INF/config/acceso_produccion.properties"));
                    System.out.println("Aqui:>>>" + getServletContext().getRealPath("/WEB-INF/config/acceso_produccion.properties"));
                    prop.store(f, null);
                    f.close();
                    System.out.println("Actualizando información");
                    cfg.setProperties(prop);
                    getServletContext().setAttribute("CFG", cfg);
                    WorkflowFactory.updateConfiguration(cfg);
                    //Actualizar parametros del filtro
                    out.println("{\"result\":\"OK\",\"message\":\"Datos grabados correctamente\"}");
                    // </editor-fold>
                    break;
                }
                case 3: {
                    // <editor-fold defaultstate="collapsed" desc="CASE 3">
                    int co_modo = Integer.parseInt(request.getParameter("co_modo"));

                    Configuration cfg = (Configuration) getServletContext().getAttribute("CFG");
                    Properties prop = cfg.getProperties();
                    prop.setProperty("app.co_modo", "" + co_modo);

                    FileOutputStream f = new FileOutputStream(getServletContext().getRealPath("/WEB-INF/config/acceso_produccion.properties"));
                    System.out.println("Aqui:>>>" + getServletContext().getRealPath("/WEB-INF/config/acceso_produccion.properties"));
                    prop.store(f, null);
                    f.close();
                    System.out.println("Actualizando información");
                    cfg.setProperties(prop);
                    getServletContext().setAttribute("CFG", cfg);
                    WorkflowFactory.updateConfiguration(cfg);
                    //Actualizar parametros del filtro
                    out.println("{\"result\":\"OK\",\"message\":\"Datos grabados correctamente\"}");
                    // </editor-fold>
                    break;
                }
                case 4: {
                    // <editor-fold defaultstate="collapsed" desc="CASE 4">
                    String formato = request.getParameter("formato");
                    String level = request.getParameter("level");

                    Configuration cfg = (Configuration) getServletContext().getAttribute("CFG");
                    Properties prop = cfg.getProperties();
                    prop.setProperty("log.formato", formato);
                    prop.setProperty("log.level", level);

                    FileOutputStream f = new FileOutputStream(getServletContext().getRealPath("/WEB-INF/config/acceso_produccion.properties"));
                    System.out.println("Aqui:>>>" + getServletContext().getRealPath("/WEB-INF/config/acceso_produccion.properties"));
                    prop.store(f, null);
                    f.close();
                    System.out.println("Actualizando información");
                    cfg.setProperties(prop);
                    getServletContext().setAttribute("CFG", cfg);
                    WorkflowFactory.updateConfiguration(cfg);
                    Escritor.actualiza_formato(formato);
                    Escritor.actualiza_loglevel(level);
                    //Actualizar parametros del filtro
                    out.println("{\"result\":\"OK\",\"message\":\"Datos grabados correctamente\"}");
                    // </editor-fold>
                    break;
                }
                case 5: {
                    // <editor-fold defaultstate="collapsed" desc="CASE 5">
                    boolean correo = Boolean.parseBoolean(request.getParameter("correo"));
                    String smtp_host = request.getParameter("smtp_host");
                    String smtp_localhost = request.getParameter("smtp_localhost");
                    boolean twitter = Boolean.parseBoolean(request.getParameter("twitter"));
                    String consumerKey = request.getParameter("consumerKey");
                    String consumerSecret = request.getParameter("consumerSecret");
                    String twitterToken = request.getParameter("twitterToken");
                    String twitterTokenSecret = request.getParameter("twitterTokenSecret");

                    Configuration cfg = (Configuration) getServletContext().getAttribute("CFG");
                    Properties prop = cfg.getProperties();
                    prop.setProperty("workflow.servicio.correo", "" + correo);
                    prop.setProperty("mail.smtp.host", "" + smtp_host);
                    prop.setProperty("mail.smtp.localhost", "" + smtp_localhost);
                    prop.setProperty("workflow.servicio.twitter", "" + twitter);
                    prop.setProperty("consumerKey", "" + consumerKey);
                    prop.setProperty("consumerSecret", "" + consumerSecret);
                    prop.setProperty("twitterToken", "" + twitterToken);
                    prop.setProperty("twitterTokenSecret", "" + twitterTokenSecret);

                    FileOutputStream f = new FileOutputStream(getServletContext().getRealPath("/WEB-INF/config/acceso_produccion.properties"));
                    System.out.println("Aqui:>>>" + getServletContext().getRealPath("/WEB-INF/config/acceso_produccion.properties"));
                    prop.store(f, null);
                    f.close();
                    System.out.println("Actualizando información");
                    cfg.setProperties(prop);
                    getServletContext().setAttribute("CFG", cfg);
                    WorkflowFactory.updateConfiguration(cfg);
                    //Actualizar parametros del filtro
                    out.println("{\"result\":\"OK\",\"message\":\"Datos grabados correctamente\"}");
                    // </editor-fold>
                    break;
                }

            }
        } catch (IOException | NumberFormatException ep) {
            System.out.println("[SC]Error:" + ep.getMessage());
            ep.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
